package com.cpe.web.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.HttpMethod;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;

import com.cpe.web.intercepter.PostInterceptor;
import com.cpe.web.intercepter.PreInterceptor;
import com.cpe.web.intercepter.SeparateInterceptor;

@Configuration
@EnableWebMvc
@ComponentScan(basePackages = "com.cpe.web.controller")
public class WebApplicationConfig extends WebMvcConfigurerAdapter {

	@Override
	public void addCorsMappings(CorsRegistry registry) {
		registry.addMapping("/**").allowedMethods(HttpMethod.DELETE.name(), HttpMethod.POST.name(),
				HttpMethod.PUT.name(), HttpMethod.GET.name());
	}

	@Override
	public void addInterceptors(InterceptorRegistry registry) {
		// // 注册前置拦截器
		// registry.addInterceptor(preInterceptor());
		//
		// // 注册分流拦截器
		// registry.addInterceptor(separateInterceptor());

		// 注册后置拦截器
		registry.addInterceptor(postInterceptor());
	}

	@Bean
	public PreInterceptor preInterceptor() {
		return new PreInterceptor();
	}

	@Bean
	public SeparateInterceptor separateInterceptor() {
		return new SeparateInterceptor();
	}

	@Bean
	public PostInterceptor postInterceptor() {
		return new PostInterceptor();
	}
}
